﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DAL_Class
{
    public class KhoaHoc
    {
        NLLDataContext db;
        #region "Thêm mới khóa học"
        public string AddNewCourse(Course ent)
        {
            db = new NLLDataContext();
            string result = string.Empty;

            db.Courses.InsertOnSubmit(ent);
            try
            {
                db.SubmitChanges();
                result = "Thêm mới thành công";
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion

        #region "Chỉnh sửa khóa học"
        public void UpdateCourse(Course ent)
        {
            db = new NLLDataContext();

            Course c = db.Courses.Single(p => p.ID == ent.ID);

            c.Name = ent.Name;
            c.ThumbImage = ent.ThumbImage;
            c.Description = ent.Description;
            c.Content = ent.Content;
            c.Quantities = ent.Quantities;
            c.StartDate = ent.StartDate;
            c.Schedule = ent.Schedule;
            c.TimeSlot = ent.TimeSlot;
            c.IsFrontPage = ent.IsFrontPage;
            c.Status = ent.Status;
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region "Xóa khóa học"
        public void DeleteCourse(int ID)
        {
            db = new NLLDataContext();
            try
            {
                //select ra checkinID trong bang RegisterCourses
                IEnumerable<int> checkinID = from c in db.RegisterCourses
                                             where c.CourseID.Equals(ID)
                                             select c.CheckInID;

                //Xoa het ban ghi co ID = checkinID trong bang CheckIns
                foreach (int checkID in checkinID)
                {
                    var delCheckIn = from CI in db.CheckIns
                                     where CI.ID.Equals(checkID)
                                     select CI;
                    db.CheckIns.DeleteAllOnSubmit(delCheckIn);
                }

                //Xoa het cac ban ghi trong RegisterCourses co CourseID = ID truyen vao
                var reg = from r in db.RegisterCourses
                          where r.CourseID.Equals(ID)
                          select r;
                db.RegisterCourses.DeleteAllOnSubmit(reg);

                //Xoa Khoa hoc co ID = ID truyen vao
                var course = from p in db.Courses
                             where p.ID.Equals(ID)
                             select p;

                db.Courses.DeleteAllOnSubmit(course);

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
